package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDTO;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author GRS
 * @since 2024/4/19 下午7:51
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController
{
    @Resource
    private SetmealService setmealService;

    @Resource
    private SetmealDishService setmealDishService;

    @Resource
    private CategoryService categoryService;

    /**
     * 新增套餐
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDTO setmealDTO)
    {
        log.info("套餐信息：{}", setmealDTO);
        setmealService.saveWithDish(setmealDTO);
        return R.success("新增套餐成功");
    }

    /**
     * 套餐分页查询
     */
    @GetMapping("/page")
    public R<IPage<SetmealDTO>> page(int page, int pageSize, String name)
    {
        // 分页构造器
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        // 添加查询条件，根据name进行模糊查询
        wrapper
            .like(name != null, Setmeal::getName, name)
            .orderByDesc(Setmeal::getUpdateTime);
        IPage<Setmeal> modelPage = setmealService.page(pageInfo, wrapper);
        IPage<SetmealDTO> setmealDTOPage = new Page<>();
        BeanUtils.copyProperties(modelPage, setmealDTOPage, "records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDTO> list = records
            .stream()
            .map(item ->
            {
                SetmealDTO setmealDTO = new SetmealDTO();
                BeanUtils.copyProperties(item, setmealDTO);
                // 分类ID
                Long categoryId = item.getCategoryId();

                // 根据分类ID查询分类对象
                Category category = categoryService.getById(categoryId);
                if (category != null)
                {
                    // 分类名称
                    String categoryName = category.getName();

                    setmealDTO.setCategoryName(categoryName);
                }
                return setmealDTO;
            }).toList();

        setmealDTOPage.setRecords(list);
        return R.success(setmealDTOPage);
    }

    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids)
    {
        setmealService.removeWithDish(ids);
        return R.success("套餐数据删除成功");
    }

    @GetMapping("/{id}")
    public R<SetmealDTO> get(@PathVariable("id") Long id)
    {
        SetmealDTO setmealDTO = new SetmealDTO();

        Setmeal one = setmealService.getById(id);
        BeanUtils.copyProperties(one, setmealDTO);

        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(wrapper);
        setmealDTO.setSetmealDishes(list);

        return R.success(setmealDTO);
    }

    /**
     * 根据条件查询套餐数据
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal)
    {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper
            .eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId())
            .eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus())
            .orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(wrapper);

        return R.success(list);
    }
}